package com.wx.cloud.user.controller;

import com.wx.cloud.user.common.CommonResult;
import com.wx.cloud.user.model.LoginInfo;
import com.wx.cloud.user.model.Page;
import com.wx.cloud.user.model.User;
import com.wx.cloud.user.service.LoginInfoService;
import com.wx.cloud.user.service.RedisService;
import com.wx.cloud.user.service.UserService;
import com.wx.cloud.user.vo.LoginParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @Autowired
    private UserService userService;

    @Autowired
    private LoginInfoService loginInfoService;

    @Autowired
    private RedisService redisService;

    @PostMapping("/register")
    public CommonResult register(@RequestBody User user){
        HashMap<String, String> data = new HashMap<>();
        String result = userService.register(user);
        data.put("data", result);
        return CommonResult.success(data);
    }

    @PostMapping("/login")
    public CommonResult login(@RequestBody LoginParam loginParam, HttpServletRequest request){

        HashMap<String, String> data = new HashMap<>();
        String checkCode = (String) redisService.get("checkCode");
        Boolean b =checkCode.toLowerCase() == null || !checkCode.toLowerCase().equals(loginParam.getCode().toLowerCase());
        if (b){
            return CommonResult.validateFailed("验证码错误或失效");
        } else {
            String token = null;
            try {
                token = userService.login(loginParam,request);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (token == "该用户的角色已被禁用"){
                return CommonResult.validateFailed("该用户的角色已被禁用");
            } else if (token == "该用户已被冻结"){
                return CommonResult.validateFailed("该用户已被冻结");
            } else {
                if (StringUtils.isEmpty(token) || token == "用户名或密码错误"){
                    return CommonResult.validateFailed("用户名或密码错误");
                } else if (token == "密码输入错误超过三次，该用户已被冻结"){
                    return CommonResult.validateFailed("密码输入错误超过三次，该用户已被冻结");
                }
                redisService.set("userCode",loginParam.getUserCode());
                data.put("tokenHead",tokenHead);
                data.put("token",token);
            }
        }
        return CommonResult.success(data);
    }

    @ApiOperation("用户登出")
    @PostMapping("/loginOut")
    public CommonResult loginOut(){
        HashMap<String, Object> data = new HashMap<>();
        Long result = redisService.del("userCode");
        data.put("result", result);
        return CommonResult.success(data);
    }

    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<LoginInfo> loginInfos = loginInfoService.getSelectList(data);
        int count = loginInfoService.getSelectCount(data);
        Page page = new Page();
        page.setList(loginInfos);
        page.setTotal(count);
        map.put("list",loginInfos);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @GetMapping("/getLoginInfoById")
    public CommonResult getLoginInfoById(@RequestParam Integer id){
        HashMap<String, Object> map = new HashMap<>();
        LoginInfo loginInfo = loginInfoService.getLoginInfoById(id);
        map.put("data",loginInfo);
        return CommonResult.success(map);
    }

    @PostMapping("/delLoginInfo")
    public CommonResult delLoginInfo(@RequestBody LoginInfo loginInfo){
        HashMap<String, Integer> map = new HashMap<>();
        int result = loginInfoService.delLoginInfo(loginInfo);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PostMapping("/batchDeletion")
    public CommonResult batchDeletion(@RequestParam List<Integer> ids){
        HashMap<String, Object> map = new HashMap<>();
        String result = loginInfoService.batchDeletion(ids);
        map.put("data",result);
        return CommonResult.success(map);
    }

}
